# -*- coding: utf-8 -*-
from utils.config import *
from utils.utils import *
import pandas as pd
import utils.MyTT as Mytt
# import logging
from utils.log import Loggers
from utils.pushService import PushService
import numpy as np

# 每小时的前5分钟 不调用脚本，防止数据不完整
should_skip()

logger = Loggers('Ema10zhen')
# logger.clear()

pushSer = PushService('10日均线插针反转策略')



# 循环处理
for instId in config_movingAverage_instId:
    # 读取300条最新的数据
    try:
        df = loadCsvData(instId,'1d',300)
    except:
        continue

    # 10日均线
    df['ema10']= Mytt.EMA(df['close'],10)
    # k线实体部分
    df['body'] = abs(df['close'] - df['open'])
    # 上影线
    df['upper_shadow'] = np.where(
        df['close'] > df['open'],
        df['high'] - df['close'],
        df['high'] - df['open']
    )
    # 下影线
    df['lower_shadow'] = np.where(
        df['close'] > df['open'],
        df['open'] - df['low'],
        df['close'] - df['low']
    )

    # 判断k线实体是否和 均线相交
    df['cross_ema'] = 1
    df.loc[(df['close'] > df['ema10']) & (df['open'] > df['ema10'])   ,'cross_ema'] = 0
    df.loc[(df['close'] < df['ema10']) & (df['open'] < df['ema10'])   ,'cross_ema'] = 0

    df['open_num'] = abs((df['open'] - df['ema10']))/df['ema10']
    df['close_num'] = abs((df['close'] - df['ema10']))/df['ema10']
    df.loc[df['open_num']>df['close_num'],'body_num' ] = df['close_num']
    df.loc[df['open_num']<df['close_num'],'body_num' ] = df['open_num']
    # df['body_num'] = df['open_num'] if df['open_num'] < df['close_num'] else df['close_num']
    # print(df.tail(10))
    # 寻找开单信号
    df['signal'] = 0 
    # 做多信号  大级别多头趋势，收盘价>10均线 ，最高价不超过布林带上轨
    df.loc[(df['close'] > df['ema10']) & (df['upper_shadow'] >= df['body']*2) & (df['cross_ema']==0) & (df['close_num']>0.04)  ,'signal'] = -1
    df.loc[(df['close'] < df['ema10']) & (df['lower_shadow'] >= df['body']*2) & (df['cross_ema']==0) & (df['close_num']>0.04)  ,'signal'] = 1
    
    # 快速计算信号近期胜率
    # shenglv5,shenglv10 = getShenglv(df)
    # 取出所有满足条件的信号数据
    df_signal = df[df['signal']!=0]
    # print(len(df_signal))
    # print(df_signal.tail(30))
    # exit()
    # 拿到最后出现信号的数据
    after_signal = df_signal.iloc[-1]
    xiaoshu = 1 if after_signal['close']>0 else 3
    # 计算止损点位
    # zhisun= round(after_signal['recent_low'] + (after_signal['atr'] if after_signal['signal'] == -1 else after_signal['atr']*-1),xiaoshu)

    fangxiang = '做多' if after_signal['signal']>0 else '做空'
    signal_text = f"{instId}{fangxiang} 最近信号：{after_signal.name} 收盘价：{after_signal['close']} "

    # 计算止损位置
    # zhisunR = round( abs((zhisun - after_signal['close'])) /after_signal['close'] *100,2)

    # signal_text+= f"止损：{zhisun}({zhisunR}%)"
    hour = getHoursPassed(after_signal.name)
    # 进行日志记录
    if hour < 25:
        # 25小时内的新信号 就可以 进行钉钉推送
        if pushSer.add_push_item(after_signal, instId,[
            f"止损：5%，止盈：ema10均线\n\n",
        ]):
            logger.info(signal_text)
    else:
        print(f"过期信号：{signal_text}")
    # 完成消息的通知
pushSer.push_message()

